<template>
  <!--修改签署密码-->
  <uni-popup ref="modifySignPwd">
    <uni-popup-dialog
        type="info"
        cancelText="取消"
        confirmText="确认"
        title="修改签署密码/支付密码"
        @close="cancel"
        @confirm="submitForm"
        before-close>
      <template v-slot="content">
<!--        <uni-notice-bar single color="#00a660" background-color="#ffffff" text="请填写新的签署密码。"/>-->
        <uni-forms
            ref="form"
            :modelValue="form"
            :rules="rules"
            :label-width="80" type="line" label-align="right"
            class="paddingAll">
          <uni-forms-item label="新密码" name="newPassword" required>
            <uni-easyinput placeholder="设置填写签署/支付密码" type="password" maxlength="18" v-model="form.newPassword"/>
          </uni-forms-item>
          <uni-forms-item label="确认密码" name="confirmPassword" required>
            <uni-easyinput placeholder="请再次填写签署/支付密码" type="password" maxlength="18" v-model="form.confirmPassword"/>
          </uni-forms-item>
        </uni-forms>
      </template>
    </uni-popup-dialog>
  </uni-popup>
</template>

<script>

import {updatePayPassword} from "@/api/ec/person/user/userInfo";

export default {
  props: {},
  data() {
    return {
      // 表单
      user: {},
      // 短信验证码
      oldSmsCode: "",
      // 登录密码
      form: {
        newPassword: null,
        confirmPassword: null
      },
      // 登录密码校验
      rules: {
        newPassword: {
          rules: [
            {required: true, errorMessage: '请输入6-18位数字字母的新密码！'},
            {minLength: 6, maxLength: 18, errorMessage: '请输入6-18位数字字母的新密码！'},
            {pattern: /^[a-zA-Z0-9]{6,18}$/, errorMessage: "请输入6-18位数字字母的新密码！"}
          ]
        },
        confirmPassword: {
          rules: [
            {required: true, errorMessage: '请输入6-18位数字字母的新密码！'},
            {
              validateFunction: function (rule, value, data, callback) {
                if (value != data.newPassword) {
                  callback('两次输入密码不一致，请重新输入')
                }
                return true
              }
            },
          ]
        },
      },
    };
  },
  methods: {
    /** 打开弹出框 */
    toOpenDialog(user, oldSmsCode) {
      this.user = user;
      this.oldSmsCode = oldSmsCode;
      this.toShowModifySignPwd();
    },
    /** 取消按钮 */
    cancel() {
      this.$refs.modifySignPwd.close()
    },
    /** 修改签署密码弹出框 */
    toShowModifySignPwd() {
      this.form = {newPassword: "", confirmPassword: ""};
      this.$refs.modifySignPwd.open('center')
    },
    /** 修改登录密码,确定按钮 */
    submitForm() {
      this.$refs.form.validate().then(valid => {
        if (valid) {
          this.$modal.loading();
          let oldSmsCode = this.oldSmsCode;
          let newPayPassword = this.form.newPassword;
          const param = {oldSmsCode, newPayPassword}
          updatePayPassword(param).then(response => {
            uni.hideLoading()
            this.cancel();
            this.$modal.msgSuccess("修改签署密码成功！");
          }).catch(() => {
            uni.hideLoading()
          });
        }
      });
    },
  }
};
</script>
<style scoped lang="scss">

::v-deep .uni-forms-item .is-required {
  display: none;
}

::v-deep .uni-popup-dialog {
  width: 85%;
}

::v-deep .uni-dialog-content {
  padding: 26rpx;
}

</style>
